<?php

class IncomingorderController extends BaseController {

//incoming Order
	public function getIncomingorder()
	{	
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('keyword');
		Session::forget('datestart');
		Session::forget('dateend');
		$incomingorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->leftjoin('sys_invoice','invoice_order_id','=','order_id')
						->where('order_status','=','Pending')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
		return View::make('admin.order.incoming.incomingorder')->with('incomingorder',$incomingorder);
	}

	public function postSetdealprice($id)
	{	
		$order = DB::table('sys_invoice')
					->join('sys_order','order_id','=','invoice_order_id')
					->join('sys_customer','order_customer_id','=','customer_id')
					->where('invoice_order_id','=',$id)
					->first();
		if(Input::get('payment')==''){
			return Redirect::to('/order/incomingorder')->with('message','Harga order order telah deal');
		}else{
			$dealprice = invoice::find($order->invoice_id);
			$dealprice->invoice_order_id = $id;
			$dealprice->invoice_total_payment = Input::get('payment');
			$dealprice->invoice_status = 'Belum lunas';
			$dealprice->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set harga order : ".$id;	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();
		}

		$user = array(
		  'email'=>$order->customer_email,
		  'nama'=>$order->customer_name
		);
		//send mail
		Mail::send('emails.pembayaran', array('pesan'=>'Total biaya pesanan anda.','ordercode'=>$order->order_code,'orderprice' => Input::get('payment')), function($message) use ($user){
		$message->to($user['email'], $user['nama'])->subject('Total biaya pesanan');
    	});
		return Redirect::to('/order/incomingorder')->with('message','Harga order telah deal');
	}

	public function postIncomingorderapprove()
	{

		$orders = DB::table('sys_order')
				->join('sys_customer','order_customer_id','=','customer_id')
				->join('sys_invoice','invoice_order_id','=','order_id')
				->get();
		$a = 0;
		foreach($orders as $order){
		 

		 if(Input::get($order->order_id)=='1'){
			$a = $a + Input::get($order->order_id);
		 	if($order->invoice_total_payment==''){
				return Redirect::to('/order/incomingorder')->with('error','Total harga (INV/Fix) belum ditentukan');
			 }
			$orders = DB::table('sys_order')
					->where('order_id','=',$order->order_id)
					->update(array('order_status' => 'Approve'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $order->order_id;
			$orderstatus->order_status_type = 'Approve';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();
			
			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set order : ".$order->order_id." Approve";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$user = array(
			  'email'=>$order->customer_email,
			  'nama'=>$order->customer_name
			);
			//send mail
			Mail::send('emails.pesananapprove', array('pesan'=>'Pesanan anda kami terima.','ordercode'=>$order->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Pesanan kami terima');
	    	});

		 }
		}
		if($a<>0){
			return Redirect::to('/order/incomingorder')->with('message','Order telah diapprove');
		}
		return Redirect::to('/order/incomingorder')->with('error','Silahkan pilih order');

		
	}

	public function getIncomingorderapproveid($id)
	{
			$orders = DB::table('sys_order')
					->join('sys_customer','order_customer_id','=','customer_id')
					->join('sys_invoice','invoice_order_id','=','order_id')
					->where('order_id','=',$id)
					->get();
			foreach($orders as $order){
			 if($order->invoice_total_payment==''){
				return Redirect::to('/order/incomingorder')->with('error','Total harga (INV/Fix) belum ditentukan');
			}

			$orders = DB::table('sys_order')
					->where('order_id','=',$id)
					->update(array('order_status' => 'Approve'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $id;
			$orderstatus->order_status_type = 'Approve';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set order : ".$id." Approve";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();


			$user = array(
			  'email'=>$order->customer_email,
			  'nama'=>$order->customer_name
			);
			//send mail
			Mail::send('emails.pesananapprove', array('pesan'=>'Pesanan anda kami terima.','ordercode'=>$order->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Pesanan kami terima');
	    	});

		}

		return Redirect::to('/order/incomingorder')->with('message','Order telah diapprove');
	}

	public function postIncomingordercancel()
	{
		$orders = DB::table('sys_order')
				->join('sys_customer','order_customer_id','=','customer_id')
				->get();
		$a = 0;
		foreach($orders as $order){
		if(Input::get($order->order_id)=='1'){
			$a = $a + Input::get($order->order_id);
			$orders = DB::table('sys_order')
					->where('order_id','=',$order->order_id)
					->update(array('order_status' => 'Cancel'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $order->order_id;
			$orderstatus->order_status_type = 'Cancel';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set cancel order : ".$order->order_id." Cancel";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			
			$user = array(
			  'email'=>$order->customer_email,
			  'nama'=>$order->customer_name
			);
			//send mail
			Mail::send('emails.pesanancancel', array('pesan'=>'Maaf pesanan anda kami tolak.','ordercode'=>$order->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Maaf, pesanan kami tolak');
	    	});
		}
		}
		
		if($a<>0){
			return Redirect::to('/order/incomingorder')->with('message','Order telah dicancel');
		}
		return Redirect::to('/order/incomingorder')->with('error','Silahkan pilih order');
	}

	public function getIncomingordercancelid($id)
	{
			$orders = DB::table('sys_order')
					->where('order_id','=',$id)
					->update(array('order_status' => 'Cancel'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $id;
			$orderstatus->order_status_type = 'Cancel';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set cancel order : ".$id." Cancel";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$cust = DB::table('sys_order')
					->join('sys_customer','order_customer_id','=','customer_id')
					->where('order_id',$id)
					->first();
			$user = array(
			  'email'=>$cust->customer_email,
			  'nama'=>$cust->customer_name
			);
			//send mail
			Mail::send('emails.pesanancancel', array('pesan'=>'Maaf pesanan anda kami tolak.','ordercode'=>$cust->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Maaf, pesanan kami tolak');
	    	});
		return Redirect::to('/order/incomingorder')->with('message','Order telah dicancel');
	}

	public function postDetailincomingorderchangedesain($id)
	{
			$orders = order::find($id);
				
			        if($orders->order_name=='brochure'){
			          	$ordername = 'brosur';
			        }else{
			        	$ordername = $orders->order_name;
			        }
         
				File::delete($orders->order_desain);
				$desain = Input::file('desain');
				$desain->move(public_path().'/design/'.$ordername, $desain->getClientOriginalName());
				$orders->order_desain = '/design/'.$ordername.'/'.$desain->getClientOriginalName();

			$orders->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Ganti desain order : ".$id;	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$cust = DB::table('sys_order')
					->join('sys_customer','order_customer_id','=','customer_id')
					->where('order_id',$id)
					->first();
			$user = array(
			  'email'=>$cust->customer_email,
			  'nama'=>$cust->customer_name
			);
			//send mail
			Mail::send('emails.pesanandesain', array('pesan'=>'Biaya tambahan desain.','ordercode'=>$cust->order_code), function($message) use ($user){
			$message->to($user['email'], $user['nama'])->subject('Biaya desain');
	    	});
		return Redirect::back()->with('message','Desain telah diubah');
	}

	public function postIncomingorder()
	{	

		if(Input::get('filter')=='date'){
			Session::forget('list');
			Session::forget('filterby');
			Session::forget('keyword');
			Session::put('filter','date');
				if(Input::get('datestart')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
				if(Input::get('dateend')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
			Session::put('datestart',Input::get('datestart'));
			Session::put('dateend',Input::get('dateend'));
			$incomingorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->leftjoin('sys_invoice','invoice_order_id','=','order_id')
						->where('order_status','=','Pending')
						->whereBetween('order_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			return View::make('admin.order.incoming.incomingorder')->with('incomingorder',$incomingorder);
		}else if(Input::get('filter')=='type'){
			Session::forget('filterby');
			Session::forget('keyword');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
			if(Input::get('typeby')=='all'){
			$incomingorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->leftjoin('sys_invoice','invoice_order_id','=','order_id')
						->where('order_status','=','Pending')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else{
			$incomingorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->leftjoin('sys_invoice','invoice_order_id','=','order_id')
						->where('order_status','=','Pending')
						->where('order_name','=',Input::get('typeby'))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}
			return View::make('admin.order.incoming.incomingorder')->with('incomingorder',$incomingorder);
		}else if(Input::get('filter')=='keyword'){
			Session::put('filter','keyword');
			Session::forget('list');
			Session::put('filterby',Input::get('filterby'));
			Session::put('keyword',Input::get('keyword'));
			if(Input::get('filterby')=='code'){
				$incomingorder = DB::table('sys_order')
							->join('sys_customer','customer_id','=','order_customer_id')
							->leftjoin('sys_invoice','invoice_order_id','=','order_id')
							->where('order_status','=','Pending')
							->where('order_code','LIKE','%'.Input::get('keyword').'%')
							->groupBy('order_id')
							->orderBy('order_date','DESC')
							->paginate(5);
				return View::make('admin.order.incoming.incomingorder')->with('incomingorder',$incomingorder);
			}else if(Input::get('filterby')=='name'){
				$incomingorder = DB::table('sys_order')
							->join('sys_customer','customer_id','=','order_customer_id')
							->leftjoin('sys_invoice','invoice_order_id','=','order_id')
							->where('order_status','=','Pending')
							->where('customer_name','LIKE','%'.Input::get('keyword').'%')
							->groupBy('order_id')
							->orderBy('order_date','DESC')
							->paginate(5);
				return View::make('admin.order.incoming.incomingorder')->with('incomingorder',$incomingorder);
			}
		}else{
        	if(Session::get('filter')=="date"){
        		$incomingorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->leftjoin('sys_invoice','invoice_order_id','=','order_id')
						->where('order_status','=','Pending')
						->whereBetween('order_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
        	}else if(Session::get('filter')=="type"){
        		if(Session::get('list')=='all'){
				$incomingorder = DB::table('sys_order')
							->join('sys_customer','customer_id','=','order_customer_id')
							->leftjoin('sys_invoice','invoice_order_id','=','order_id')
							->where('order_status','=','Pending')
							->groupBy('order_id')
							->orderBy('order_date','DESC')
							->paginate(5);
				}else{
				$incomingorder = DB::table('sys_order')
							->join('sys_customer','customer_id','=','order_customer_id')
							->leftjoin('sys_invoice','invoice_order_id','=','order_id')
							->where('order_status','=','Pending')
							->where('order_name','=',Session::get('list'))
							->groupBy('order_id')
							->orderBy('order_date','DESC')
							->paginate(5);
				}
        	}else if(Session::get('filter')=="keyword"){
        		if(Session::get('filterby')=='code'){
					$incomingorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->leftjoin('sys_invoice','invoice_order_id','=','order_id')
								->where('order_status','=','Pending')
								->where('order_code','LIKE','%'.Session::get('keyword').'%')
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					return View::make('admin.order.incoming.incomingorder')->with('incomingorder',$incomingorder);
				}else if(Session::get('filterby')=='name'){
					$incomingorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->leftjoin('sys_invoice','invoice_order_id','=','order_id')
								->where('order_status','=','Pending')
								->where('customer_name','LIKE','%'.Session::get('keyword').'%')
								->groupBy('order_id')
								->orderBy('order_date','DESC')
							->paginate(5);
				return View::make('admin.order.incoming.incomingorder')->with('incomingorder',$incomingorder);
			}
        	}
        }
		return View::make('admin.order.incoming.incomingorder')->with('incomingorder',$incomingorder);
		



	}



}